package com.mazaiting.classfile.attr.entity;

/**
 * 节点对实体
 *
 * @author mazaiting
 * @since 2021/8/13
 */

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.constant.ConstantPool;

import java.util.Arrays;

/**
 * 节点对
 */
public final class ElementValuePairsEntity {
    /**
     * 常量池
     */
    private final ConstantPool pool;
    /**
     * 节点名索引 无符号 u2
     * must be a valid index into the constant_pool table
     */
    private final int elementNameIndex;
    /**
     * 节点值
     */
    private final ElementValueEntity value;

    public ElementValuePairsEntity(ClassReader reader, ConstantPool pool) {
        this.pool = pool;
        this.elementNameIndex = reader.readU2ToUnsignedShort();
        this.value = ElementValueEntity.read(reader, pool);
    }

    /**
     * 获取节点名
     * @return 节点名
     */
    public String getElementName() {
        return pool.getUtf8(elementNameIndex);
    }

    @Override
    public String toString() {
        return "ElementValuePairsEntity{" +
                "pool=" + pool +
                ", elementNameIndex=" + elementNameIndex +
                ", value=" + value +
                '}';
    }

}
















